﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProjectStorage.DocumentGenerator.CodeTransformations
{
    public class CommaSeparetedProjectList
    {

        static readonly string SEPARATOR = ",";
        static readonly string QUOTE = "'";

        protected List<ProjectStorageLib.Project.ProjectManager> projectList;

        public CommaSeparetedProjectList(List<ProjectStorageLib.Project.ProjectManager> projectList)
        {
            this.projectList = projectList;
        }

        public string Generate()
        {
            StringBuilder result = new StringBuilder();

            if (projectList != null && projectList.Count > 0)
            {
                var factors = projectList.First().Factors.ToList();
                var practices = projectList.First().AgilePractices.ToList();

                int counter = 0;
                foreach (var item in projectList)
                {
                    counter++;

                    result.Append(inQuotes(counter));
                    factors.ForEach((f) =>
                    {
                        result.Append(SEPARATOR);
                        var res = item.GetScaleValueByFactor(f);
                        result.Append(inQuotes(res != null ? res.FactorScale.Order : null));
                    });

                    practices.ForEach(p =>
                    {
                        result.Append(SEPARATOR);
                        var res = item.GetPracticeValueByPractice(p);
                        result.Append(inQuotes(res != null ? res.value.ToString() : null));
                    });

                    result.AppendLine();
                }
            }

            return result.ToString();
        }

        private string inQuotes(Object obj)
        {
            return string.Format("{0}{1}{2}", QUOTE, obj, QUOTE);
        }

    }
}
